/**
 * (C) copyright geolabs 2006
 * author: Gérald Fenoy
 */

/**
 * LDAPView class
 */
LDAPView=Controler.extend({ _id: 0 });
LDAPView.define({

  root: "channel",

  rootValues: { 
    ct: "title",
    description: "description",
    ci: { src: "image.url", title: "image.title" }
  },

  rootElements: { 
    ct: "div",
    description: "div",
    ci: "img"
  },

  loop:"item",
  loopNode:"div",

  loopValues: { 
    cit: "title", 
    cil0: { href: "link" },
    cil1: { href: "link" },
    cieu: { src: "enclosure.url" },
    cid: "description",
    cip: "pubDate"
  },

  loopElements: { 
    cit: "div", 
    cil0: "a",
    cil1: "a",
    cieu: "img",
    cid: "div",
    cip: "span"
  },

  htmlValues: Array("description","cid"),

  tree: {},

  _init: function(){
      this.rootNode=(arguments.length>0&&arguments[0].root)?arguments[0].root:document.body;
      this.id=LDAPView._id;
      LDAPView._id++;
      this.superCall("_init",arguments);
  },

  update: function(xmlJson){
    this.execRequest("./js/LDAPView.xml");
    this.xmlJson=xmlJson;
    this.tree=xmlJson.tree;
  },

  /**
   * Clean the content of view as xml result if there are more than once
   * element corresponding to the one which is searched.
   *
   */
  clean: function(){
      var curr=this.request.responseXML.getElementsByTagName(i);
      for(var j=1;j<curr.length;j++)
	curr[j].parentNode.removeChild(curr[j]);
    },

  setValue: function(tagname,val,el,prefix,index){

      this.clean();
      
      var oldElement=this.request.responseXML.getElementsByTagName(tagname)[0];
      
      if(oldElement){
	var newElement;
	newElement=document.createElement(el[tagname]);
	newElement.setAttribute("class",tagname);
	newElement.setAttribute("id",tagname+"_"+index);

	if(typeof val[tagname] == "object"){
	  var j=0;
	  for(j in val[tagname]){
	    if(val[tagname][j]){
	      try{
		var tmp=eval(prefix+""+val[tagname][j]);
		if(tmp==undefined){
		  throw("unable to use this attribute");
		}
		newElement.setAttribute(j,tmp);
	      }
	      catch(e){
		//alert("ok");
		oldElement.setAttribute("style","display: none");
		//oldElement.setAttribute("id",""+e);
		try{
		  var plop=oldElement.parentNode;
		  plop.removeChild(plop.getElementsByTagName(tagname)[0]);
		}catch(e){alert(e);}
		return;
	      }
	    }
	  }
	}
	else{

	  var tmp=eval(prefix+val[tagname]);
	  //newElement.setAttribute("class",tagname);
	  //newElement.setAttribute("id",tagname+"_"+index);
	  var test=1;
	  for(var t=0;t<this.htmlValues.length;t++)
	    if(tagname==this.htmlValues[t]){
	      test=-1;
	      break;
	    }
	  //alert(test+" => "+tagname);
	  if(test>0)
	    newElement.appendChild(document.createTextNode(""+tmp));
	  else{
	    //alert(i+" "+oldElement.parentNode+"=>"+index);
	    //alert("[AJAX_DEBUG] must set the id of the parentNode");
	    tmp=HTMLEntity.decode(tmp);

	    tmp="<div>"+tmp+"</div>";
	    var toAdd=(new DOMParser()).parseFromString(tmp, "text/xml");

	    for(var i=0;i<toAdd.childNodes[0].childNodes.length;i++){
	      var toAppend=null;
	      if(toAdd.childNodes[0].childNodes[i].nodeName=="#text"){
		toAppend=document.createTextNode(toAdd.childNodes[0].childNodes[i].nodeValue);
	      }
	      else{
		toAppend=toAdd.childNodes[0].childNodes[i];
	      }
	      newElement.innerHTML+=(new XMLSerializer()).serializeToString(toAppend);
	    }
	  }

	}
	
	if(oldElement.childNodes)
	  for(var t=0;t<oldElement.childNodes.length;t++){
	    try{
	      newElement.appendChild(oldElement.childNodes[t].cloneNode(true));
	    }catch(e){alert(e)}
	  }

	var test=oldElement.parentNode.getAttribute("idx");
	if(test){
	  //alert(test+"_"+this.identity+"_"+index);
	  oldElement.parentNode.setAttribute("id",test+"_"+this.id+"_"+index);
	}

	//oldElement.parentNode.appendChild(newElement);
	oldElement.parentNode.replaceChild(newElement,oldElement);

	//return newElement.parentNode;
	
      }

      //return oldElement;

    },

    newData: function(){

      looproot=this.request.responseXML.getElementsByTagName(this.loop)[0];

      /**
       * ROOT ELEMENTS
       */
      var i=0;
      for(i in this.rootValues){
	this.setValue(i,this.rootValues,this.rootElements,"this.tree."+this.root+".","",-1);
      }

      /**
       * LOOP
       */
      var parent=looproot.parentNode;
      
      var test1=document.createElement("div");
      for(i=0;i<looproot.childNodes.length;i++){
	
	try{test1.appendChild(looproot.childNodes[i]);}
	catch(e){
	  try{
	    test1.innerHTML+=(new XMLSerializer()).serializeToString(looproot.childNodes[i]);
	  }
	  catch(e){alert(i+" "+e+" "+looproot.childNodes[i]+test1);}
	}
	
      }

      var swap=looproot.childNodes[0].cloneNode(true);
      for(var t=1;t<looproot.childNodes[0].length;t++){
	if(looproot.childNodes[t].nodeName!="#text"){
	  swap=looproot.childNodes[t].cloneNode(true);
	  break;
	}
      }

      parent.replaceChild(swap.cloneNode(true),looproot);

      for(var j=0;j<this.xmlJson.elements.length;j++){
	var i=0;
	for(i in this.loopElements){
	  this.setValue(i,this.loopValues,this.loopElements,"this.tree."+this.root+"."+this.loop+j+".",j);
	}
	if(j+1<this.xmlJson.elements.length){
	  parent.appendChild(swap.cloneNode(true));
	}
      }

      this.rootNode.innerHTML+=(new XMLSerializer()).serializeToString(this.request.responseXML.childNodes[0]);

    }
    
    });
